<?php

/**
 * excelParser actions.
 *
 * @package    rbis
 * @subpackage parser
 * @author     Nor� Moreno, Fahmi Machda
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class parserActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }

  public function executeUpload(sfWebRequest $request)
  {
    $this->file = $request->getParameter('file');
    $file = $this->file;
	
	/*
    $reader = new sfCsvReader($file);

    $reader->open();

    while ($data = $reader->read())
    {
        $this->data = $data;
    }

    $reader->close();
	*/
  }


  public function executeXml(sfWebRequest $request)
  {
    if (!$speciesList = $request->getParameter('speciesList'))
    {
        die("No XML document can be created");
    }

    $rbisXml = new SimpleXMLElement('<rbis></rbis>');

    $speciesObjList = Doctrine::getTable('RbisSpecies')->getSpeciesList($speciesList);

    foreach ($speciesObjList as $speciesObj){
        $speciesNode= $rbisXml->addChild('species');
        $speciesNode->addAttribute('id',$speciesObj->getId());
        $name= $speciesNode->addChild('name');
        $name->addChild('family',$speciesObj->getFamily());
        $name->addChild('genusSpecies',$speciesObj->getName());
        $speciesNode->addChild('icon', $speciesObj->getIcon());

        $recordsList = $speciesObj->getRbisRecords();


        foreach ($recordsList as $recordObj){
            $recordNode= $speciesNode->addChild('record');
            $recordNode->addChild('quantity', $recordObj->getQuantity());
            $recordNode->addChild('depth', $recordObj->getDepth());
            $recordNode->addChild('zone', $recordObj->getZone());

            $stationObj = $recordObj->getRbisStation();
            $stationNode= $recordNode->addChild('station');
            $stationNode->addChild('name', $stationObj->getName());
            $stationNode->addChild('latitude', $stationObj->getLatitude());
            $stationNode->addChild('longitude', $stationObj->getLongitude());
            $stationNode->addChild('temperature', $stationObj->getTemperature());
            $stationNode->addChild('salinity', $stationObj->getSalinity());
        }

    }

    
    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $domnode = dom_import_simplexml($rbisXml);
    $domnode = $doc->importNode($domnode, true);
    $domnode = $doc->appendChild($domnode);

    //echo $this->doc->saveXML();
    $userId = $this->getUser()->getAttribute('id');
    $this->filename  = 'tmp/'.$userId.'.xml';
    $doc->save("assets/".$this->filename);
    

    //in case we stick with simpeXML
    //$rbisXml->asXML("assets/".$this->filename);
  }
}
